export function BF(nums: number[], max: number): number[][] {
  const open: { cap: number; index: number[] }[] = [];

  nums.forEach((v) => {
    while (!f()) {
      open.push({ cap: 0, index: [] });
    }
    function f() {
      let tempCap: number = max + 1;
      let tempIndex: number = -1;

      open.forEach((vv, i) => {
        const k = max - vv.cap - v;

        if (k >= 0) {
          if (k < tempCap) {
            tempCap = k;
            tempIndex = i;
          }
        }
      });

      if (tempIndex < 0) {
        return false;
      }

      open[tempIndex].cap += v;
      open[tempIndex].index.push(v);

      return true;
    }
  });

  return open.map((v) => v.index);
}
